package mongo

import (
	"github.com/spf13/viper"
	"gopkg.in/mgo.v2"
)

type collection struct {
	Collection *mgo.Collection
}

//DB:esf_logs
//ColName:admin.albumlog
func NewCollection(DB, ColName string) (*collection, error) {
	url := viper.GetString("mongo." + DB + ".url")
	if url == "" {
		url = viper.GetString("mongo.default_url")
	}

	session, e := newMongo(url)
	if e != nil {
		return nil, e
	}

	col := &collection{
		Collection: session.DB(DB).C(ColName),
	}

	return col, nil
}

func (col *collection) Insert(insert any) error {
	return col.Collection.Insert(insert)
}

func (col *collection) Update(selector map[string]any, update any) error {
	return col.Collection.Update(selector, update)
}

func (col *collection) Delete(selector map[string]any) error {
	return col.Collection.Remove(selector)
}

func (col *collection) Find(selector map[string]any) *mgo.Query {
	return col.Collection.Find(selector)
}
